<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-09-22 10:26:19
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-09-24 13:06:25
 * @Description: 
 * 
-->
<template>
  <el-dialog
    draggable
    overflow
    v-model="visible"
    :title="titleMap[mode]"
    :width="800"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
      <sk-tabs type="border-card">
        <el-tab-pane label="承兑信息">
          <el-row>
            <el-col :span="6">
              <el-form-item label="单据编号" prop="code">
                <el-input v-model="form.code" disabled placeholder="自动编写"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="单据日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="付款金额" prop="amount">
                <el-input v-model="form.amount" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="收款单位" prop="customer_id">
                <skAutoChoose
                  v-model="form.customer.name"
                  :name="'customer'"
                  :assign="form"
                  @select="selectCustomer"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="部门" prop="department_id">
                <sk-select-group
                  v-model="form.department_id"
                  :url="$API.system.financeDepartment.treelist"
                ></sk-select-group>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="父科目">
                <sk-select-group v-model="parent_subject_id" :list="parent_subjectList" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="科目" prop="subject_id">
                <sk-select-group v-model="form.subject_id" :list="children_subjectList" />
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="应付单位" prop="supplier_id">
                <skAutoChoose
                  v-model="form.supplier.name"
                  :name="'factory'"
                  :assign="form"
                  :asKey="'supplier_id'"
                  @select="selectSupplier"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="应付抬头" prop="handlebilltrade_id">
                <sk-select-group
                  v-model="form.handlebilltrade_id"
                  :list="handlebilltradeList"
                ></sk-select-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" rows="3"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <sc-VxeTable
            ref="xGrid"
            :gridOps="gridOps"
            :gridEvts="gridEvents"
            :tablecode="'finance_acceptance_pay_save'"
            :data="form.details"
          >
          </sc-VxeTable>
        </el-tab-pane>
      </sk-tabs>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
  <skBrowserChoose
    v-model="openChooseDlg"
    :name="'acceptance_browser'"
    @close="openChooseDlg = false"
    @select="additems"
  >
  </skBrowserChoose>
</template>

<script>
import skSelectGroup from "@/components/skSelectGroup";
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import skBrowserChoose from "@/components/skBrowserChoose";
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skSelectGroup, skDicSelect, skAutoChoose, skBrowserChoose },
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      subjectList: [],
      openChooseDlg: false,
      handlebilltradeList: [],
      parent_subject_id: 2202,
      gridEvents: {
        choose: () => {
          this.openChooseDlg = true;
        },
      },
      gridOps: {
        height: 200,
      },
      form: {
        id: "",
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        amount: 0,
        subject_id: 2202,
        details: [],
        customer: {},
        supplier: {},
      },
      rules: {
        billdate: { required: true, message: "请选择单据日期" },
        amount: { required: true, message: "票面金额不能为0" },
      },
      visible: false,
      isSaveing: false,
    };
  },

  mounted() {
    this.getSubject();
  },
  watch: {
    "form.details": {
      handler(val) {
        this.form.amount = val.reduce((sum, v) => parseFloat(v.saleamount) + sum, 0);
      },
      deep: true,
    },
  },
  computed: {
    parent_subjectList() {
      return this.subjectList.map(v => {
        return { id: v.id, name: v.name };
      });
    },
    children_subjectList() {
      let data = this.subjectList.filter(v => {
        return this.parent_subject_id == null || v.id == this.parent_subject_id;
      });
      return data;
    },
  },
  methods: {
    additems(records) {
      records.filter(data => {
        data["saleamount"] = data["leftamount"];
        data["acceptance_rec_id"] = data["id"];
        delete data["id"];
        this.form.details.push(data);
      });
      this.openChooseDlg = false;
    },
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    getSubject() {
      this.$API.basedata.subject.menulist.get().then(res => {
        this.subjectList = this.recursionSetName(res.data);
      });
    },
    recursionSetName(v) {
      v = v.map(v => {
        v.name = "├" + v.code + "-" + v.name;
        v.children = this.recursionSetName(v.children);
        return v;
      });
      return v;
    },

    selectSupplier(data) {
      this.handlebilltradeList = data?.billtrade ?? [];
      this.form.customer_id = 0;
      this.form.customer = {};
    },
    selectCustomer() {
      this.form.supplier_id = 0;
      this.form.supplier = {};
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.finance.acceptance_pay.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    getDataInfo(id) {
      this.$API.finance.acceptance_pay.info.get({ id: id }).then(res => {
        if (res.code == 200) {
          res.data.supplier && this.selectSupplier(res.data.supplier);
          Object.assign(this.form, this.$TOOL.removeNull(res.data));
        }
      });
    },
    //表单注入数据
    setData(data) {
      this.getDataInfo(data.id);
    },
  },
};
</script>
<style></style>
